

if (SERVER) then

	AddCSLuaFile( "shared.lua" )
	SWEP.Weight			= 5
	SWEP.AutoSwitchTo		= false
	SWEP.AutoSwitchFrom		= false

end

if ( CLIENT ) then

	SWEP.WeaponSelectIconLetter	= "B"
	SWEP.DrawAmmo			= true
	//SWEP.DrawCrosshair		= false
	SWEP.DrawWeaponInfoBox		= true					// Should draw the weapon info box
	SWEP.BounceWeaponIcon		= false					// Should the weapon icon bounce?
	SWEP.SwayScale			= 1.0					// The scale of the viewmodel sway
	SWEP.BobScale			= 1.0					// The scale of the viewmodel bob
	SWEP.WepSelectIcon		= surface.GetTextureID( "weapons/swep" )
	SWEP.ViewModelFOV		= 75
	SWEP.ViewModelFlip		= false
	SWEP.CSMuzzleFlashes		= false

	// This is the font that's used to draw the death icons
	surface.CreateFont( "csd", ScreenScale( 30 ), 500, true, true, "CSKillIcons" )
	surface.CreateFont( "csd", ScreenScale( 60 ), 500, true, true, "CSSelectIcons" )
	surface.CreateFont( "Counter-Strike", ScreenScale( 60 ), 500, true, true, "CSSelectIconsRyno" )

end

SWEP.Author				= "RynO-SauruS"
SWEP.Contact				= ""
SWEP.Purpose				= ""
SWEP.Instructions			= ""

SWEP.Category				= "RynO-SauruS"

SWEP.Spawnable				= false
SWEP.AdminSpawnable			= false

SWEP.UseCustomMuzzleFlash		= true
SWEP.MuzzleEffect			= "CSSMuzzleFlashX"
SWEP.MuzzleAttachment			= "1"

SWEP.ShakeWeaponSelectIcon		= false

//SWEP.TracerFreq			= 0
SWEP.TracerType                 	= "Tracer"

//Some Possible Tracer Values:
// Tracer
// AR2Tracer
// HelicopterTracer
// AirboatGunTracer
// AirboatGunHeavyTracer

SWEP.InfiniteAmmo                       = false
SWEP.UseScope                           = false
SWEP.WeaponDeploySpeed                  = 1

//SWEP.ZoomedPrimaryAutomatic		= false
//SWEP.ZoomedPrimaryDelay		= 10
//SWEP.ZoomedPrimaryCone		= 10
//SWEP.ZoomedPrimaryDamage		= 10
//SWEP.ZoomedPrimaryRecoil		= 0.2
//SWEP.ZoomedTracerFreq			= 1
//SWEP.ZoomedDrawCrosshair		= false

//SWEP.UnzoomedPrimaryAutomatic		= false
//SWEP.UnzoomedPrimaryDelay		= 0.15
//SWEP.UnzoomedPrimaryCone		= 0.02
//SWEP.UnzoomedPrimaryDamage		= 40
//SWEP.UnzoomedPrimaryRecoil		= 0.2
//SWEP.UnzoomedTracerFreq		= 0
//SWEP.UnzoomedDrawCrosshair		= false

SWEP.Primary.BulletForce                = "5"
SWEP.Primary.WeaponDrawSound            = ""
SWEP.Primary.CustomReloadSound          = ""
SWEP.Primary.Sound			= "Weapon_AK47.Single"
//SWEP.Primary.Recoil			= 0.2
//SWEP.Primary.Damage			= 40
SWEP.Primary.NumShots			= 1
//SWEP.Primary.Cone			= 0.02
//SWEP.Primary.Delay			= 0.15

SWEP.Primary.ClipSize			= -1
SWEP.Primary.DefaultClip		= -1
//SWEP.Primary.Automatic		= false
SWEP.Primary.Ammo			= "smg1"

SWEP.Secondary.ClipSize			= -1
SWEP.Secondary.DefaultClip		= -1
SWEP.Secondary.Automatic		= false
SWEP.Secondary.Ammo			= "none"

//End of Default Config

//Custom Stuff


/*---------------------------------------------------------
   Name: SWEP:Deploy( )
   Desc: Whip it out
---------------------------------------------------------*/
function SWEP:Deploy()
        self.Primary.Automatic = self.UnzoomedPrimaryAutomatic
        self.Primary.Delay = self.UnzoomedPrimaryDelay
        self.Primary.Cone = self.UnzoomedPrimaryCone
        self.Primary.Damage = self.UnzoomedPrimaryDamage
        self.Primary.Recoil = self.UnzoomedPrimaryRecoil
        self.TracerFreq = self.UnzoomedTracerFreq
	self.DrawCrosshair = self.UnzoomedDrawCrosshair
        self.ScopeLevel = 0
	self.Weapon:SetNetworkedBool( "Zoomed", false )
        self.Weapon:EmitSound( Sound( self.Primary.WeaponDrawSound ) )
	return true
end


/*---------------------------------------------------------
---------------------------------------------------------*/
function SWEP:Initialize()

	if ( SERVER ) then
		self:SetWeaponHoldType( self.HoldType )
		self:SetNPCMinBurst( 30 )
		self:SetNPCMaxBurst( 30 )
		self:SetNPCFireRate( 0.01 )
	end

        self.Primary.Automatic = self.UnzoomedPrimaryAutomatic
        self.Primary.Delay = self.UnzoomedPrimaryDelay
        self.Primary.Cone = self.UnzoomedPrimaryCone
        self.Primary.Damage = self.UnzoomedPrimaryDamage
        self.Primary.Recoil = self.UnzoomedPrimaryRecoil
        self.TracerFreq = self.UnzoomedTracerFreq
	self.DrawCrosshair = self.UnzoomedDrawCrosshair
        self.ScopeLevel = 0

        self.Weapon:SetDeploySpeed( self.WeaponDeploySpeed )

	self.Weapon:SetNetworkedBool( "Zoomed", false )

	self.Weapon:SetNetworkedBool( "Ironsights", false )

end


/*---------------------------------------------------------
	Reload is being pressed
---------------------------------------------------------*/
function SWEP:Reload()
        self.Weapon:DefaultReload( ACT_VM_RELOAD )
        if (self.UseScope == true) then
                if ( (self.ScopeLevel == 1) or (self.ScopeLevel == 2) ) then
                        if self.Weapon:DefaultReload() then
                                self.Primary.Automatic = self.UnzoomedPrimaryAutomatic
                                self.Primary.Delay = self.UnzoomedPrimaryDelay
                                self.Primary.Cone = self.UnzoomedPrimaryCone
                                self.Primary.Damage = self.UnzoomedPrimaryDamage
                                self.Primary.Recoil = self.UnzoomedPrimaryRecoil
                                self.TracerFreq = self.UnzoomedTracerFreq
				self.DrawCrosshair = self.UnzoomedDrawCrosshair
                                if (SERVER) then
                                        self.Owner:SetFOV( self.OriginalFOV, 0.3 )
                                end
				self:SetZoomed(false)
                                self.ScopeLevel = 0
                        end
                end
        end
	if self.Weapon:DefaultReload() then
                self.Weapon:EmitSound( Sound( self.Primary.CustomReloadSound ) )
	end
	self:SetIronsights( false )
end


/*---------------------------------------------------------
   Runs every tick
---------------------------------------------------------*/
function SWEP:Think()
end


/*---------------------------------------------------------
	PrimaryAttack
---------------------------------------------------------*/
function SWEP:PrimaryAttack()

	self.Weapon:SetNextSecondaryFire( CurTime() + self.Primary.Delay )
	self.Weapon:SetNextPrimaryFire( CurTime() + self.Primary.Delay )
	
	if ( !self:CanPrimaryAttack() ) then return end
	
	// Play shoot sound
	self.Weapon:EmitSound( Sound( self.Primary.Sound ) )
	
	// Shoot the bullet
	self:CSShootBullet( self.Primary.Damage, self.Primary.Recoil, self.Primary.NumShots, self.Primary.Cone )
	
	// Remove 1 bullet from our clip
	if ( self.InfiniteAmmo == false ) then
		self:TakePrimaryAmmo( 1 )
	end
	
	if ( self.Owner:IsNPC() ) then return end
	
	// Punch the player's view
	self.Owner:ViewPunch( Angle( math.Rand(-0.2,-0.1) * self.Primary.Recoil, math.Rand(-0.1,0.1) *self.Primary.Recoil, 0 ) )
	
	// In singleplayer this function doesn't get called on the client, so we use a networked float
	// to send the last shoot time. In multiplayer this is predicted clientside so we don't need to 
	// send the float.
	if ( (SinglePlayer() && SERVER) || CLIENT ) then
		self.Weapon:SetNetworkedFloat( "LastShootTime", CurTime() )
	end
	
end

/*---------------------------------------------------------
   Name: SWEP:PrimaryAttack( )
   Desc: +attack1 has been pressed
---------------------------------------------------------*/
function SWEP:CSShootBullet( dmg, recoil, numbul, cone )

	numbul 	= numbul 	or 1
	cone 	= cone 		or 0.01

	local bullet = {}
	bullet.Num 		= numbul
	bullet.Src 		= self.Owner:GetShootPos()	// Source
	bullet.Dir 		= self.Owner:GetAimVector()	// Dir of bullet
	bullet.Spread 	= Vector( cone, cone, 0 )		// Aim Cone
	bullet.Tracer	= self.TracerFreq
	bullet.TracerName	= self.TracerType
	bullet.Force	= self.Primary.BulletForce		// Amount of force to give to phys objects
	bullet.Damage	= dmg
	

	//Muzzle Flash from Teta_Bonita's 'Realistic' SWEP Base

	local PlayerPos = self.Owner:GetShootPos()
	local PlayerAim = self.Owner:GetAimVector()

	local fx = EffectData()
	fx:SetEntity(self.Weapon)
	fx:SetOrigin(PlayerPos)
	fx:SetNormal(PlayerAim)
	fx:SetAttachment(self.MuzzleAttachment)
	if (self.UseCustomMuzzleFlash == true) then
		util.Effect(self.MuzzleEffect,fx)
	else
		self.Owner:MuzzleFlash()
	end

	self.Owner:FireBullets( bullet )
	self.Weapon:SendWeaponAnim( ACT_VM_PRIMARYATTACK )	// View model animation
	self.Owner:SetAnimation( PLAYER_ATTACK1 )		// 3rd Person Animation
	
	if ( self.Owner:IsNPC() ) then return end
	
	// CUSTOM RECOIL !
	if ( (SinglePlayer() && SERVER) || ( !SinglePlayer() && CLIENT && IsFirstTimePredicted() ) ) then
	
		local eyeang = self.Owner:EyeAngles()
		eyeang.pitch = eyeang.pitch - recoil
		self.Owner:SetEyeAngles( eyeang )
	
	end

end


/*---------------------------------------------------------
	Checks the objects before any action is taken
	This is to make sure that the entities haven't been removed
---------------------------------------------------------*/
function SWEP:DrawWeaponSelection( x, y, wide, tall, alpha )

	draw.SimpleText( self.WeaponSelectIconLetter, "CSSelectIconsRyno", x + wide/2, y + tall*0.2, Color( 255, 189, 6, 255 ), TEXT_ALIGN_CENTER )

	// try to fool them into thinking they're playing a Tony Hawks game
	if (self.ShakeWeaponSelectIcon == true) then
		draw.SimpleText( self.WeaponSelectIconLetter, "CSSelectIconsRyno", x + wide/2 + math.Rand(-4, 4), y + tall*0.2+ math.Rand(-14, 14), Color( 255, 210, 0, math.Rand(10, 120) ), TEXT_ALIGN_CENTER )
		draw.SimpleText( self.WeaponSelectIconLetter, "CSSelectIconsRyno", x + wide/2 + math.Rand(-4, 4), y + tall*0.2+ math.Rand(-9, 9), Color( 255, 210, 0, math.Rand(10, 120) ), TEXT_ALIGN_CENTER )
	end

end

local IRONSIGHT_TIME = 0.25

/*---------------------------------------------------------
   Name: GetViewModelPosition
   Desc: Allows you to re-position the view model
---------------------------------------------------------*/
function SWEP:GetViewModelPosition( pos, ang )

	if ( !self.IronSightsPos ) then return pos, ang end

	local bIron = self.Weapon:GetNetworkedBool( "Ironsights" )
	
	if ( bIron != self.bLastIron ) then
	
		self.bLastIron = bIron 
		self.fIronTime = CurTime()
		
		if ( bIron ) then 
			self.SwayScale 	= 0.3
			self.BobScale 	= 0.1
		else 
			self.SwayScale 	= 1.0
			self.BobScale 	= 1.0
		end
	
	end
	
	local fIronTime = self.fIronTime or 0

	if ( !bIron && fIronTime < CurTime() - IRONSIGHT_TIME ) then 
		return pos, ang 
	end
	
	local Mul = 1.0
	
	if ( fIronTime > CurTime() - IRONSIGHT_TIME ) then
	
		Mul = math.Clamp( (CurTime() - fIronTime) / IRONSIGHT_TIME, 0, 1 )
		
		if (!bIron) then Mul = 1 - Mul end
	
	end

	local Offset	= self.IronSightsPos
	
	if ( self.IronSightsAng ) then
	
		ang = ang * 1
		ang:RotateAroundAxis( ang:Right(), 		self.IronSightsAng.x * Mul )
		ang:RotateAroundAxis( ang:Up(), 		self.IronSightsAng.y * Mul )
		ang:RotateAroundAxis( ang:Forward(), 	self.IronSightsAng.z * Mul )
	
	
	end
	
	local Right 	= ang:Right()
	local Up 		= ang:Up()
	local Forward 	= ang:Forward()
	
	

	pos = pos + Offset.x * Right * Mul
	pos = pos + Offset.y * Forward * Mul
	pos = pos + Offset.z * Up * Mul

	return pos, ang
	
end


/*---------------------------------------------------------
	SetIronsights
---------------------------------------------------------*/
function SWEP:SetIronsights( b )

	self.Weapon:SetNetworkedBool( "Ironsights", b )

end


SWEP.NextSecondaryAttack = 0
/*---------------------------------------------------------
	SecondaryAttack
---------------------------------------------------------*/
function SWEP:SecondaryAttack()

        if (self.UseScope == true) then
                if(self.ScopeLevel == 0) then
                        self.Primary.Automatic = self.ZoomedPrimaryAutomatic
                        self.Primary.Delay = self.ZoomedPrimaryDelay
                        self.Primary.Cone = self.ZoomedPrimaryCone
                        self.Primary.Damage = self.ZoomedPrimaryDamage
                        self.Primary.Recoil = self.ZoomedPrimaryRecoil
                        self.TracerFreq = self.ZoomedTracerFreq
			self.DrawCrosshair = self.ZoomedDrawCrosshair
                        if (SERVER) then
                                self.OriginalFOV = self.Owner:GetFOV()
                                self.Owner:SetFOV( 25, 0.3 )
                        end
			self.Weapon:EmitSound( Sound( "Default.Zoom" ) )
			self:SetZoomed(true)
                        self.ScopeLevel = 1
                else if(self.ScopeLevel == 1) then
                        self.Primary.Automatic = self.ZoomedPrimaryAutomatic
                        self.Primary.Delay = self.ZoomedPrimaryDelay
                        self.Primary.Cone = self.ZoomedPrimaryCone
                        self.Primary.Damage = self.ZoomedPrimaryDamage
                        self.Primary.Recoil = self.ZoomedPrimaryRecoil
                        self.TracerFreq = self.ZoomedTracerFreq
			self.DrawCrosshair = self.ZoomedDrawCrosshair
                        if (SERVER) then
                                self.Owner:SetFOV( 15, 0.2 )
                        end
			self.Weapon:EmitSound( Sound( "Default.Zoom" ) )
			self:SetZoomed(true)
                        self.ScopeLevel = 2
                else
                        self.Primary.Automatic = self.UnzoomedPrimaryAutomatic
                        self.Primary.Delay = self.UnzoomedPrimaryDelay
                        self.Primary.Cone = self.UnzoomedPrimaryCone
                        self.Primary.Damage = self.UnzoomedPrimaryDamage
                        self.Primary.Recoil = self.UnzoomedPrimaryRecoil
                        self.TracerFreq = self.UnzoomedTracerFreq
			self.DrawCrosshair = self.UnzoomedDrawCrosshair
                        if (SERVER) then
                                self.Owner:SetFOV( self.OriginalFOV, 0.3 )
                        end
			self.Weapon:EmitSound( Sound( "Default.Zoom" ) )
			self:SetZoomed(false)
                        self.ScopeLevel = 0
                        end
                end
        end

	if ( !self.IronSightsPos ) then return end
	if ( self.NextSecondaryAttack > CurTime() ) then return end
	
	bIronsights = !self.Weapon:GetNetworkedBool( "Ironsights", false )
	
	self:SetIronsights( bIronsights )
	
	self.NextSecondaryAttack = CurTime() + 0.3
	
end

function SWEP:SetZoomed( b )

	self.Weapon:SetNetworkedBool( "Zoomed", b )

end

function SWEP:AdjustMouseSensitivity()
	if self.ScopeLevel == 0 then
		return -1
	end
	if self.ScopeLevel == 1 then
		return 0.1
	end
	if self.ScopeLevel == 2 then
		return 0.001
	end
end

/*---------------------------------------------------------
	DrawHUD
	
	Just a rough mock up showing how to draw your own crosshair.
	
---------------------------------------------------------*/
function SWEP:DrawHUD()

	// No crosshair when ironsights is on
	if ( self.Weapon:GetNetworkedBool( "Ironsights" ) ) then return end

	local x = ScrW() / 2.0
	local y = ScrH() / 2.0
	local scale = 10 * self.Primary.Cone
	
	// Scale the size of the crosshair according to how long ago we fired our weapon
	local LastShootTime = self.Weapon:GetNetworkedFloat( "LastShootTime", 0 )
	scale = scale * (2 - math.Clamp( (CurTime() - LastShootTime) * 5, 0.0, 1.0 ))
	
	surface.SetDrawColor( 0, 255, 0, 255 )
	
	// Draw an awesome crosshair
	local gap = 40 * scale
	local length = gap + 20 * scale
	surface.DrawLine( x - length, y, x - gap, y )
	surface.DrawLine( x + length, y, x + gap, y )
	surface.DrawLine( x, y - length, x, y - gap )
	surface.DrawLine( x, y + length, x, y + gap )

	//Scope stuff
	if ( self.Weapon:GetNetworkedBool( "Zoomed" ) ) then
		surface.SetDrawColor( 0, 0, 0, 255 )
		surface.DrawLine( 0, y, ScrW(), y )
		surface.DrawLine( x, 0, x, ScrH() )
		surface.SetTexture(surface.GetTextureID("weapons/scopes/scope"))
		surface.SetDrawColor( 255, 255, 255, 255 )
		surface.DrawTexturedRect( x - (ScrH() / 2), 0, ScrH(), ScrH() )
		surface.SetDrawColor( 0, 0, 0, 255 )
		surface.DrawRect(0, 0, x - (ScrH() / 2), ScrH() )
		surface.DrawRect(x + (ScrH() / 2), 0, ScrW() - (x + (ScrH() / 2)), ScrH() )
	end

end

/*---------------------------------------------------------
	onRestore
	Loaded a saved game (or changelevel)
---------------------------------------------------------*/
function SWEP:OnRestore()

	self.NextSecondaryAttack = 0
	self:SetIronsights( false )
	self:SetZoomed(false)
	
end















































